package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.model.bo.GoodsBO;
import com.biu.billiards.model.bo.OpenDoorBO;
import com.biu.billiards.model.entity.Goods;
import com.biu.billiards.model.entity.OpenDoor;
import com.biu.billiards.mapper.OpenDoorMapper;
import com.biu.billiards.model.vo.OpenDoor.AddOpenDoorVo;
import com.biu.billiards.model.vo.OpenDoor.GetOpenDoorVo;
import com.biu.billiards.model.vo.OpenDoor.UpdateOpenDoorVO;
import com.biu.billiards.service.IOpenDoorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-11-08
 */
@Service
public class OpenDoorServiceImpl extends ServiceImpl<OpenDoorMapper, OpenDoor> implements IOpenDoorService {

    @Resource
    private OpenDoorMapper openDoorMapper;


    @Override
    public IPage<OpenDoorBO> getOpenDoorPage(GetOpenDoorVo getOpenDoorVo) {


        QueryWrapper<OpenDoor> query = new QueryWrapper<>();

        //新建分页条件
        Page<OpenDoor> p = new Page<>(getOpenDoorVo.getCurrentPage(),getOpenDoorVo.getPageSize());
        IPage<OpenDoor> userIPage = openDoorMapper.selectPage(p,query);
        IPage<OpenDoorBO> page = new Page<>();
        List<OpenDoorBO> adminDetailBOList = new ArrayList<>();
        for (OpenDoor admin: userIPage.getRecords()){
            OpenDoorBO adminDetailBO = new OpenDoorBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;


    }

    @Override
    public Boolean updateOpenDoor(UpdateOpenDoorVO updateOpenDoorVO) {


        QueryWrapper<OpenDoor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id",updateOpenDoorVO.getOpenId());

        OpenDoor openDoor = openDoorMapper.selectOne(queryWrapper);
        openDoor.setPhone(updateOpenDoorVO.getPhone());
        openDoor.setShopId(updateOpenDoorVO.getShopId());


        return openDoorMapper.update(openDoor, queryWrapper) == 1;
    }

    @Override
    public Boolean addOpenDoor(AddOpenDoorVo addOpenDoorVo) {
        OpenDoor openDoor = new OpenDoor();
        BeanUtils.copyProperties(addOpenDoorVo,openDoor);

        return openDoorMapper.insert(openDoor) == 1;
    }

    @Override
    public Boolean deleteOpenDoor(Integer deviceId) {
        QueryWrapper<OpenDoor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id",deviceId);
        return openDoorMapper.delete(queryWrapper) == 1;
    }
}
